package com.juguo.charginganimation.Data;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.juguo.charginganimation.Bean.PhotoBean;
import com.juguo.charginganimation.MainApplication;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.utils.ToastUtil;

import cn.lemon.view.adapter.BaseViewHolder;

public class CardRecordHolder extends BaseViewHolder<PhotoBean.Photos> implements View.OnClickListener{

    private TextView beep;
    private TextView contentp;
    private ImageButton address;
    private ImageButton imageButtonQd;
    private ImageButton imageButtonZt;
    public CardRecordHolder(ViewGroup parent) {
        super(parent, R.layout.holder_consume);
    }

    private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    public void setData(final PhotoBean.Photos photoBean) {
        super.setData(photoBean);
        //getsData();
        beep.setText(photoBean.getName());
        contentp.setText(photoBean.getStDesc());
        try {
            mediaPlayer.setDataSource(photoBean.getContent());//指定音频文件路径
            mediaPlayer.setLooping(true);//设置为循环播放
            mediaPlayer.prepare();//初始化播放器MediaPlayer
        } catch (Exception e) {
            e.printStackTrace();
        }
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存
                SharedPreferences sp =MainApplication.getsInstance().getSharedPreferences("chongdian", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("chong_dian", photoBean.getContent()).apply();
                ToastUtil.showToast(MainApplication.getsInstance(),"应用成功");
            }
        });
    }

    @Override
    public void onInitializeView() {
        super.onInitializeView();
        beep = findViewById(R.id.tv_tsy);
        contentp = findViewById(R.id.tv_nr);
        address = findViewById(R.id.imageButton2);
        imageButtonQd = findViewById(R.id.imageButton_qd);
        imageButtonZt = findViewById(R.id.imageButton_zt);
        imageButtonQd.setOnClickListener(this::onClick);
        imageButtonZt.setOnClickListener(this::onClick);
    }

    @Override
    public void onItemViewClick(PhotoBean.Photos photoBean) {
        super.onItemViewClick(photoBean);
        //点击事件
        Log.i("CardRecordHolder", "onItemViewClick...................");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageButton_qd:
                imageButtonZt.setVisibility(View.VISIBLE);
                //如果没在播放中，立刻开始播放。
                if(!mediaPlayer.isPlaying()){
                    mediaPlayer.start();
                }
                //播放完成监听
                mediaPlayer.setOnCompletionListener(mp -> {
                    imageButtonQd.setVisibility(View.VISIBLE);
                    imageButtonZt.setVisibility(View.GONE);
                    mediaPlayer.pause();
                });
                break;
            case R.id.imageButton_zt:
                imageButtonQd.setVisibility(View.VISIBLE);
                imageButtonZt.setVisibility(View.GONE);
                //如果在播放中，立刻暂停。
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }
                break;
        }
    }


    /**
     * 销毁播放器
     */
    public void onDestroyed() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();//释放资源
            mediaPlayer = null;
        }
    }

}